import { useEffect } from "react";

const { ipcRenderer } = window.require("electron");

/*
 * keyCallbackMap {
 *  'create-file':()=>{
 *
 *  },
 * ...
 * }
 */
const useIpcRenderer = (keyCallbackMap) => {
    useEffect(() => {
        Object.keys(keyCallbackMap).forEach((key) => {
            ipcRenderer.on(key, keyCallbackMap[key]);
        });

        // 清除监听
        return () => {
            Object.keys(keyCallbackMap).forEach((key) => {
                ipcRenderer.removeListener(key, keyCallbackMap[key]);
            });
        };
    });
};
export default useIpcRenderer;
